﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Frame.Lib.Core.Module;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;


public partial class Admin_Module_Edit : PageEdit
{
    private ModuleBO Module
    {
        get
        {
            return ModuleBO.Instance<ModuleBO>();
        }
    }   
    private string ModuleID
    {
        get
        {
            if (Request["ID"] != null)
            {
                return StrUtil.DecryptDES(Request["ID"]);
            }
            else
            {
                return null;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ModuleID != null && ModuleID != "")
            {
                DispInfo();
            }
            else
            {
                UpperID.Value = StrUtil.DecryptDES(Request["UpperID"]);
                lblUpperName.Text = Module.GetName(UpperID.Value);
                Orders.Text = Module.GetMaxOrder().ToString();
            }
            
        }
        AddAttr();
    }

   

    private void AddAttr()
    {
        imgIcon.Attributes.Add("onclick", "SetIcon(this,'" + IconUrl.ClientID + "')");

        imgIcon.Style.Add("cursor", "pointer");
        lnkIcon.NavigateUrl = "javascript:SetIcon(document.getElementById('" + imgIcon.ClientID + "'),'" + IconUrl.ClientID + "')";

    }

    private void DispInfo()
    {
        ModuleInfo info = Module.GetInfo(ModuleID);
        EntityUtil.SetControl(pnlMain, info);
        if (info.UpperID == 0)
        {
            lblUpperName.Text = "顶级模块没有上级模块";
        }
        else
        {
            lblUpperName.Text = Module.GetName(info.UpperID);
        }
        imgIcon.ImageUrl = info.IconUrlAbsPath;
        IconUrl.Value = info.IconUrl;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        int upperID = int.Parse(UpperID.Value);
        bool blnFlag = Module.Exist(upperID, Name.Text, ModuleID);
        if (blnFlag)
        {
            JSUtil.Alert("该模块已存在");
        }
        else
        {
            ModuleInfo info = Module.GetInfo(ModuleID);
            if (Orders.Text.Trim() == "")
            {
                Orders.Text = "0";
            }
            EntityUtil.SetEntity(pnlMain, info);

            if (upperID != Constants.TREE_ROOT_UPPER_ID)
            {
                ModuleInfo parentInfo = Module.GetInfo(upperID);               
                info.Depth = (int)parentInfo.Depth + 1;
                info.Path = parentInfo.Path.ToString() + parentInfo.ID + ",";               
            }

            if (string.IsNullOrEmpty(ModuleID))
            {
                info.State = (int)ModuleState.Enable;
            }

            Module.Save(info);

            JSUtil.RefreshView();
        }
    }   
}
